package com.hpin.mlxteacher.child;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.FamilyAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.Family;
import com.hpin.mlxteacher.bean.FamilyResultInfo;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
import com.hpin.mlxteacher.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * 家庭详情页面
 * Created by Administrator on 2015/11/6.
 */
public class FamilyDetailsActivity extends BaseActivity implements View.OnClickListener{

    private ImageView iv_left;
    private TextView tv_home_condition;
    private TextView tv_family_adress;
    private TextView tv_account_type;
    private TextView tv_account_adress;
    private TextView tv_title;
    private String id;
    private ListViewForScrollView lv_family;
    private ScrollView sl_view;
    private List<Family> families = new ArrayList<Family>();
    private FamilyAdapter familyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activty_family_details);

        mApplication.addActivity(this);

        id = getIntent().getStringExtra("id");

        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取家庭信息
        getFamilyInfo();

    }

    /**
     * 获取家庭信息
     */
    private void getFamilyInfo() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("id", id);

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.GETCHILDFAMILYINFO,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    FamilyResultInfo resultInfo = JSONObject.parseObject(str, FamilyResultInfo.class);

                    if (Constant.SUCCESS == resultInfo.code) {

                        if (resultInfo.data!=null) {

                            tv_home_condition.setText(resultInfo.data.houseType);//房产情况
                            tv_family_adress.setText(resultInfo.data.address);//地址
                            tv_account_type.setText(resultInfo.data.hukouType);//户口类型
                            tv_account_adress.setText(resultInfo.data.hukouAddress);//户口地址

                            families = resultInfo.data.family;
                        }

                        if (families != null) {
                            familyAdapter.setFamilies(families);
                            familyAdapter.notifyDataSetChanged();
                        }

                    } else {
                        showToast(Constant.showErrorMsg(resultInfo.code, FamilyDetailsActivity.this));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, null);
    }
    private void init() {

        iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.family_detail));

        lv_family = (ListViewForScrollView) findViewById(R.id.lv_family);
        sl_view = (ScrollView) findViewById(R.id.sl_view);

        familyAdapter = new FamilyAdapter(this, families);
        lv_family.setAdapter(familyAdapter);
//
//        //滑动删除
//        SwipeMenuCreator creator = new SwipeMenuCreator() {
//            @Override
//            public void create(SwipeMenu menu) {
//
//                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
//
//                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
//
//                deleteItem.setWidth(DensityUtil.dip2px(FamilyDetailsActivity.this, 80));
//
//                deleteItem.setIcon(R.drawable.ic_delete);
//
//                menu.addMenuItem(deleteItem);
//
//            }
//        };

//        lv_family.setMenuCreator(creator);

        View footView = View.inflate(this, R.layout.layout_foot_family_detail, null);

        lv_family.addFooterView(footView);

//        lv_family.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
//                switch (index) {
//                    case 0:
////                         delete
////                        LogUtil.e("tag", "=======shanchu=======" + position);
//
//                        if (families!=null && families.size()>0) {
//                            families.remove(position);
////                            deleteData();
//                        }
//
//                        break;
//                }
//
//                return false;
//
//            }
//        });

        tv_home_condition = (TextView) footView.findViewById(R.id.tv_home_condition);
        tv_family_adress = (TextView) footView.findViewById(R.id.tv_family_adress);
        tv_account_type = (TextView) footView.findViewById(R.id.tv_account_type);
        tv_account_adress = (TextView) footView.findViewById(R.id.tv_account_adress);

        iv_left.setOnClickListener(this);
    }

//    private void deleteData() {
//        final FamilyPeopleDeleteBean familyPeopleDeleteBean = new FamilyPeopleDeleteBean();
//        familyPeopleDeleteBean.token = sp.getString(Constant.token,"");
////        familyPeopleDeleteBean.childId = ;
//
//        String json = JSONObject.toJSONString(familyPeopleDeleteBean);
//        HttpUtil.setPostRequest("删除中...", this, Constant.WEB_ROOT + "", json, getClass().getName(), new HttpUtil.SuccessListener() {
//
//            @Override
//            public void setOnSuccessListener(String str) {
//                if (CommonUtils.isNull(str)){
//                    showToast(Constant.NODATA);
//                    return;
//                }
//                try{
//                    FamilyPeopleDeleteRes familyPeopleDeleteRes = JSONObject.parseObject(str,FamilyPeopleDeleteRes.class);
//
//                    if (familyPeopleDeleteRes.code==Constant.SUCCESS){
//
//                        showToast("删除成功!");
//                        getFamilyInfo();
//
//                    }else{
//                        showToast(Constant.showErrorMsg(familyPeopleDeleteRes.code,FamilyDetailsActivity.this));
//                    }
//
//                }catch (Exception e){
//
//                }
//            }
//        },null);
//
//    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){

            case R.id.iv_left:
                finish();
                break;
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }
}
